<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>抖音视频评论热点分析</title>
    
    <!-- 外部依赖 -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.7.2/socket.io.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/particles.js/2.0.0/particles.min.js"></script>
    
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            overflow: hidden;
            background: linear-gradient(135deg, #1e3c72 0%, #2a5298 50%, #667eea 100%);
            color: #fff;
            height: 100vh;
        }
        
        /* 粒子背景 */
        #particles-js {
            position: fixed;
            width: 100%;
            height: 100%;
            top: 0;
            left: 0;
            z-index: 1;
        }
        
        /* 主容器 */
        .main-container {
            position: relative;
            width: 100vw;
            height: 100vh;
            z-index: 2;
            display: flex;
            flex-direction: column;
        }
        
        /* 顶部标题区域 */
        .header {
            padding: 15px 30px;
            text-align: center;
            background: linear-gradient(135deg, rgba(0, 0, 0, 0.4), rgba(30, 60, 114, 0.3));
            backdrop-filter: blur(15px);
            border-bottom: 2px solid rgba(78, 205, 196, 0.3);
            position: relative;
            overflow: hidden;
        }
        
        .header::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.1), transparent);
            animation: shine 3s infinite;
        }
        
        @keyframes shine {
            0% { left: -100%; }
            100% { left: 100%; }
        }
        
        .main-title {
            font-size: 2.2rem;
            font-weight: 800;
            background: linear-gradient(135deg, #00f5ff, #ff0080, #00ff88, #ffaa00);
            background-size: 300% 300%;
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            animation: gradient-flow 2s ease-in-out infinite;
            text-shadow: 0 0 30px rgba(0, 245, 255, 0.5);
            position: relative;
            z-index: 20;
            margin: 0;
            filter: drop-shadow(0 3px 6px rgba(0, 0, 0, 0.8));
        }
        
        @keyframes gradient-flow {
            0%, 100% { background-position: 0% 50%; }
            50% { background-position: 100% 50%; }
        }
        
        .subtitle {
            font-size: 1rem;
            margin-top: 8px;
            opacity: 1;
            color: #00f5ff;
            text-shadow: 0 2px 8px rgba(0, 0, 0, 0.8), 0 0 20px rgba(0, 245, 255, 0.3);
            position: relative;
            z-index: 25;
            font-weight: 500;
        }
        
        /* 主要内容区域 */
        .main-content {
            flex: 1;
            display: flex;
            gap: 20px;
            padding: 20px;
        }
        
        /* 评论列表容器 */
        .comments-container {
            flex: 1;
            background: linear-gradient(135deg, rgba(255, 255, 255, 0.1), rgba(30, 60, 114, 0.1));
            border-radius: 25px;
            backdrop-filter: blur(20px);
            border: 2px solid rgba(78, 205, 196, 0.3);
            overflow: hidden;
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.3), 0 0 30px rgba(78, 205, 196, 0.1);
            position: relative;
        }
        
        .comments-container::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 3px;
            background: linear-gradient(90deg, #ff0080, #00f5ff, #00ff88, #ffaa00);
            background-size: 300% 100%;
            animation: gradient-flow 3s ease-in-out infinite;
        }
        
        .comments-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 20px 30px;
            border-bottom: 2px solid rgba(78, 205, 196, 0.2);
            background: linear-gradient(135deg, rgba(0, 0, 0, 0.4), rgba(30, 60, 114, 0.3));
            backdrop-filter: blur(10px);
            position: relative;
        }
        
        .comments-title {
            font-size: 1.5rem;
            color: #00f5ff;
            margin: 0;
            font-weight: 700;
            text-shadow: 0 3px 6px rgba(0, 0, 0, 0.8), 0 0 20px rgba(0, 245, 255, 0.4);
            position: relative;
            z-index: 2;
        }
        
        .sort-controls {
            display: flex;
            gap: 10px;
            align-items: center;
        }
        
        .sort-select {
            padding: 10px 15px;
            border: 2px solid rgba(0, 245, 255, 0.4);
            border-radius: 12px;
            background: linear-gradient(135deg, 
                rgba(0, 0, 0, 0.6), 
                rgba(0, 245, 255, 0.1)
            );
            color: #fff;
            font-size: 0.9rem;
            font-weight: 600;
            text-shadow: 0 1px 2px rgba(0, 0, 0, 0.8);
            transition: all 0.3s ease;
            cursor: pointer;
        }
        
        .sort-select:focus {
            outline: none;
            border-color: #00f5ff;
            box-shadow: 0 0 15px rgba(0, 245, 255, 0.4);
            background: linear-gradient(135deg, 
                rgba(0, 0, 0, 0.7), 
                rgba(0, 245, 255, 0.15)
            );
        }
        
        .comments-list {
            height: calc(100vh - 200px);
            overflow-y: auto;
            padding: 20px;
        }
        
        .comment-item {
            background: linear-gradient(135deg, rgba(255, 255, 255, 0.08), rgba(78, 205, 196, 0.05));
            border-radius: 18px;
            padding: 25px;
            margin-bottom: 18px;
            border: 1px solid rgba(78, 205, 196, 0.2);
            transition: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
            position: relative;
            overflow: hidden;
            backdrop-filter: blur(10px);
        }
        
        .comment-item::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(78, 205, 196, 0.1), transparent);
            transition: left 0.5s ease;
        }
        
        .comment-item:hover::before {
            left: 100%;
        }
        
        .comment-item:hover {
            background: linear-gradient(135deg, rgba(255, 255, 255, 0.15), rgba(78, 205, 196, 0.1));
            transform: translateY(-5px) scale(1.02);
            box-shadow: 0 20px 40px rgba(78, 205, 196, 0.3), 0 0 20px rgba(255, 107, 107, 0.2);
            border-color: rgba(78, 205, 196, 0.4);
        }
        
        .comment-content {
            font-size: 1.05rem;
            line-height: 1.7;
            margin-bottom: 20px;
            color: #f8f9fa;
            text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
            position: relative;
            z-index: 2;
        }
        
        .comment-stats {
            display: flex;
            gap: 25px;
            font-size: 0.95rem;
            position: relative;
            z-index: 2;
        }
        
        .stat-item {
            display: flex;
            align-items: center;
            gap: 8px;
            color: #FFD700;
            background: rgba(0, 0, 0, 0.2);
            padding: 8px 12px;
            border-radius: 20px;
            border: 1px solid rgba(255, 215, 0, 0.3);
            transition: all 0.3s ease;
            backdrop-filter: blur(5px);
        }
        
        .stat-item:hover {
            background: rgba(255, 215, 0, 0.1);
            transform: scale(1.05);
            box-shadow: 0 5px 15px rgba(255, 215, 0, 0.3);
        }
        
        .stat-number {
            font-weight: 700;
            color: #4ECDC4;
            text-shadow: 0 0 10px rgba(78, 205, 196, 0.5);
        }
        
        /* 右侧边栏 */
        .right-sidebar {
            width: 350px;
            display: flex;
            flex-direction: column;
            gap: 20px;
        }
        
        /* 控制面板 */
        .control-panel {
            background: linear-gradient(135deg, 
                rgba(15, 15, 35, 0.95), 
                rgba(25, 25, 55, 0.90)
            );
            border-radius: 25px;
            padding: 25px;
            border: 2px solid rgba(0, 245, 255, 0.4);
            backdrop-filter: blur(25px);
            box-shadow: 
                0 25px 50px rgba(0, 0, 0, 0.6), 
                0 0 30px rgba(0, 245, 255, 0.3),
                inset 0 1px 1px rgba(255, 255, 255, 0.1);
            position: relative;
            overflow: hidden;
        }
        
        .control-panel::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: linear-gradient(135deg, 
                rgba(0, 245, 255, 0.05), 
                rgba(255, 0, 128, 0.05)
            );
            border-radius: 25px;
            z-index: -1;
            pointer-events: none;
        }
        
        .control-title {
            font-size: 1.4rem;
            margin-bottom: 20px;
            text-align: center;
            color: #00f5ff;
            text-shadow: 0 2px 8px rgba(0, 0, 0, 0.8), 0 0 15px rgba(0, 245, 255, 0.4);
            font-weight: 700;
            position: relative;
            z-index: 2;
        }
        
        .input-group {
            margin-bottom: 20px;
        }
        
        .input-label {
            display: block;
            margin-bottom: 8px;
            font-weight: 700;
            color: #ffffff;
            text-shadow: 0 2px 4px rgba(0, 0, 0, 0.8);
            position: relative;
            z-index: 2;
            font-size: 0.95rem;
        }
        
        .url-input {
            width: 100%;
            padding: 16px 20px;
            border: 2px solid rgba(78, 205, 196, 0.3);
            border-radius: 15px;
            background: linear-gradient(135deg, rgba(255, 255, 255, 0.1), rgba(0, 0, 0, 0.2));
            color: #fff;
            font-size: 1rem;
            transition: all 0.4s ease;
            backdrop-filter: blur(10px);
            position: relative;
            z-index: 3;
        }
        
        .url-input:focus {
            outline: none;
            border-color: #00f5ff;
            box-shadow: 0 0 25px rgba(0, 245, 255, 0.4), 0 0 10px rgba(78, 205, 196, 0.3);
            background: linear-gradient(135deg, rgba(255, 255, 255, 0.15), rgba(0, 245, 255, 0.1));
            transform: scale(1.02);
        }
        
        .url-input::placeholder {
            color: rgba(255, 255, 255, 0.6);
        }
        
        .start-button {
            width: 100%;
            padding: 18px;
            background: linear-gradient(135deg, #ff0080, #00f5ff, #00ff88);
            background-size: 200% 200%;
            border: none;
            border-radius: 15px;
            color: white;
            font-size: 1.1rem;
            font-weight: 700;
            cursor: pointer;
            transition: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
            text-transform: uppercase;
            letter-spacing: 1px;
            position: relative;
            overflow: hidden;
            text-shadow: 0 2px 6px rgba(0, 0, 0, 0.8);
            box-shadow: 0 8px 25px rgba(255, 0, 128, 0.3);
            z-index: 10;
        }
        
        .start-button::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
            transition: left 0.5s ease;
        }
        
        .start-button:hover::before {
            left: 100%;
        }
        
        .start-button:hover {
            transform: translateY(-4px) scale(1.02);
            box-shadow: 0 20px 40px rgba(255, 0, 128, 0.4), 0 0 30px rgba(0, 245, 255, 0.3);
            background-position: 100% 0;
        }
        
        .start-button:active {
            transform: translateY(-2px) scale(0.98);
        }
        
        .start-button:disabled {
            opacity: 0.6;
            cursor: not-allowed;
            transform: none;
        }
        
        /* 过滤条件样式 */
        .filter-section {
            margin: 20px 0;
            padding: 20px;
            background: linear-gradient(135deg, 
                rgba(0, 0, 0, 0.3), 
                rgba(0, 245, 255, 0.05)
            );
            border-radius: 18px;
            border: 2px solid rgba(0, 245, 255, 0.2);
            backdrop-filter: blur(10px);
            position: relative;
            z-index: 2;
        }
        
        .filter-title {
            color: #00f5ff;
            margin-bottom: 15px;
            font-size: 1.1rem;
            text-align: center;
            font-weight: 600;
            text-shadow: 0 2px 4px rgba(0, 0, 0, 0.8);
        }
        
        .filter-row {
            display: flex;
            gap: 15px;
        }
        
        .filter-item {
            flex: 1;
        }
        
        .filter-label {
            display: block;
            margin-bottom: 8px;
            font-size: 0.9rem;
            color: #ffffff;
            font-weight: 600;
            text-shadow: 0 1px 2px rgba(0, 0, 0, 0.8);
        }
        
        .filter-input {
            width: 100%;
            padding: 12px;
            border: 2px solid rgba(0, 245, 255, 0.3);
            border-radius: 12px;
            background: linear-gradient(135deg, 
                rgba(0, 0, 0, 0.4), 
                rgba(0, 245, 255, 0.1)
            );
            color: #fff;
            font-size: 0.95rem;
            text-align: center;
            transition: all 0.3s ease;
            font-weight: 600;
            text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
            position: relative;
            z-index: 3;
        }
        
        .filter-input:focus {
            outline: none;
            border-color: #00f5ff;
            box-shadow: 0 0 20px rgba(0, 245, 255, 0.4);
            background: linear-gradient(135deg, 
                rgba(0, 0, 0, 0.5), 
                rgba(0, 245, 255, 0.15)
            );
            transform: scale(1.02);
        }
        
        /* 进度条 */
        .progress-section {
            margin-top: 20px;
            display: none;
        }
        
        .progress-bar {
            width: 100%;
            height: 28px;
            background: rgba(0, 0, 0, 0.3);
            border-radius: 15px;
            overflow: hidden;
            margin: 15px 0;
            border: 1px solid rgba(78, 205, 196, 0.3);
            position: relative;
        }
        
        .progress-bar::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: linear-gradient(90deg, rgba(78, 205, 196, 0.1), rgba(255, 0, 128, 0.1));
            animation: progress-glow 2s ease-in-out infinite alternate;
        }
        
        @keyframes progress-glow {
            0% { opacity: 0.3; }
            100% { opacity: 0.7; }
        }
        
        .progress-fill {
            height: 100%;
            background: linear-gradient(90deg, #ff0080, #00f5ff, #00ff88);
            background-size: 200% 100%;
            width: 0%;
            transition: width 0.8s cubic-bezier(0.25, 0.46, 0.45, 0.94);
            border-radius: 15px;
            position: relative;
            animation: progress-shine 2s ease-in-out infinite;
        }
        
        @keyframes progress-shine {
            0%, 100% { background-position: 0% 0%; }
            50% { background-position: 100% 0%; }
        }
        
        .progress-text {
            font-size: 0.95rem;
            margin: 12px 0;
            color: #ffffff;
            text-align: center;
            font-weight: 600;
            text-shadow: 0 2px 4px rgba(0, 0, 0, 0.8);
            position: relative;
            z-index: 2;
        }
        

        
        /* 统计信息 */
        .stats-panel {
            background: linear-gradient(135deg, 
                rgba(15, 15, 35, 0.95), 
                rgba(25, 25, 55, 0.90)
            );
            border-radius: 20px;
            padding: 25px;
            border: 2px solid rgba(0, 245, 255, 0.4);
            backdrop-filter: blur(25px);
            box-shadow: 
                0 25px 50px rgba(0, 0, 0, 0.6), 
                0 0 30px rgba(0, 245, 255, 0.3),
                inset 0 1px 1px rgba(255, 255, 255, 0.1);
            position: relative;
            z-index: 2;
        }
        
        .stats-item {
            margin: 15px 0;
            text-align: center;
            position: relative;
            z-index: 2;
        }
        
        .stats-number {
            font-size: 2.2rem;
            font-weight: 800;
            color: #00f5ff;
            text-shadow: 0 3px 6px rgba(0, 0, 0, 0.8), 0 0 20px rgba(0, 245, 255, 0.4);
            margin-bottom: 5px;
        }
        
        .stats-label {
            font-size: 0.95rem;
            color: #ffffff;
            font-weight: 600;
            text-shadow: 0 2px 4px rgba(0, 0, 0, 0.8);
        }
        
        /* 空状态 */
        .empty-state {
            text-align: center;
            color: #ffffff;
            font-size: 1.2rem;
            padding: 60px 20px;
            text-shadow: 0 2px 4px rgba(0, 0, 0, 0.8);
        }
        
        .empty-icon {
            font-size: 4rem;
            margin-bottom: 20px;
            opacity: 0.8;
            filter: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.5));
            animation: float-icon 3s ease-in-out infinite;
        }
        
        @keyframes float-icon {
            0%, 100% { transform: translateY(0px); }
            50% { transform: translateY(-10px); }
        }
        
        /* 响应式设计 */
        @media (max-width: 1200px) {
            .main-content {
                flex-direction: column;
                gap: 15px;
            }
            
            .right-sidebar {
                width: 100%;
            }
            
            .comments-list {
                height: 400px;
            }
            
            .filter-row {
                flex-direction: column;
                gap: 10px;
            }
        }
    </style>
</head>
<body>
    <!-- 粒子背景 -->
    <div id="particles-js"></div>
    
    <div class="main-container">
        <!-- 顶部标题 -->
        <header class="header">
            <h1 class="main-title">抖音视频评论热点分析</h1>
        </header>
        
        <div class="main-content">
            <!-- 评论列表区域 -->
            <div class="comments-container">
                <div class="comments-header">
                    <h3 class="comments-title">📝 评论内容</h3>
                    <div class="sort-controls">
                        <select id="sort-select" class="sort-select">
                            <option value="replies">按回复数排序</option>
                            <option value="likes">按点赞数排序</option>
                        </select>
                    </div>
                </div>
                
                <div id="comments-list" class="comments-list">
                    <div class="empty-state">
                        <div class="empty-icon">📋</div>
                        <p>输入视频链接开始采集评论</p>
                    </div>
                </div>
            </div>
            
            <!-- 右侧控制面板 -->
            <div class="right-sidebar">
                <!-- 控制面板 -->
                <div class="control-panel">
                    <h3 class="control-title">🎯 数据采集</h3>
                    
                    <div class="input-group">
                        <label class="input-label">📺 视频链接</label>
                        <input type="text" id="video-url" class="url-input" 
                               placeholder="请输入视频URL...">
                    </div>
                    
                    <div class="filter-section">
                        <h4 class="filter-title">🔍 过滤条件</h4>
                        <div class="filter-row">
                            <div class="filter-item">
                                <label class="filter-label">👍 最小点赞</label>
                                <input type="number" id="min-likes" class="filter-input" 
                                       value="0" min="0" placeholder="0">
                            </div>
                            <div class="filter-item">
                                <label class="filter-label">💬 最小回复</label>
                                <input type="number" id="min-replies" class="filter-input" 
                                       value="0" min="0" placeholder="0">
                            </div>
                        </div>
                    </div>
                    
                    <button id="start-button" class="start-button">
                        🚀 开始分析
                    </button>
                    
                    <!-- 进度显示 -->
                    <div id="progress-section" class="progress-section">
                        <div class="progress-bar">
                            <div id="progress-fill" class="progress-fill"></div>
                        </div>
                        <div id="progress-text" class="progress-text">准备中...</div>
                    </div>
                </div>
                
                <!-- 统计面板 -->
                <div class="stats-panel">
                    <div class="stats-item">
                        <div id="comment-count" class="stats-number">0</div>
                        <div class="stats-label">有效评论</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <script>
        // 初始化粒子背景
        particlesJS('particles-js', {
            particles: {
                number: { value: 80 },
                color: { value: '#ffffff' },
                shape: { type: 'circle' },
                opacity: { value: 0.3 },
                size: { value: 3 },
                move: { enable: true, speed: 1, direction: 'none' }
            },
            interactivity: {
                detect_on: 'canvas',
                events: {
                    onhover: { enable: true, mode: 'repulse' }
                }
            }
        });
        
        // WebSocket连接
        const socket = io();
        
        // DOM元素
        const videoUrlInput = document.getElementById('video-url');
        const minLikesInput = document.getElementById('min-likes');
        const minRepliesInput = document.getElementById('min-replies');
        const startButton = document.getElementById('start-button');
        const progressSection = document.getElementById('progress-section');
        const progressFill = document.getElementById('progress-fill');
        const progressText = document.getElementById('progress-text');
        const commentsList = document.getElementById('comments-list');
        const sortSelect = document.getElementById('sort-select');
        const commentCount = document.getElementById('comment-count');
        
        // 存储当前评论数据
        let currentComments = [];
        
        // 开始分析按钮事件
        startButton.addEventListener('click', function() {
            const videoUrl = videoUrlInput.value.trim();
            const minLikes = parseInt(minLikesInput.value) || 0;
            const minReplies = parseInt(minRepliesInput.value) || 0;
            
            if (!videoUrl) {
                alert('请输入视频URL！');
                return;
            }
            
            // 显示进度
            progressSection.style.display = 'block';
            startButton.disabled = true;
            startButton.textContent = '🔄 分析中...';
            
            // 清空评论列表和统计
            commentsList.innerHTML = '<div class="empty-state"><div class="empty-icon">⏳</div><p>正在采集数据...</p></div>';
            commentCount.textContent = '0';
            currentComments = [];
            
            // 发送WebSocket请求（包含过滤条件）
            socket.emit('start_crawl', { 
                video_url: videoUrl,
                min_likes: minLikes,
                min_replies: minReplies
            });
        });
        
        // WebSocket事件监听
        socket.on('crawl_started', function(data) {
            progressText.textContent = data.message;
        });
        
        socket.on('crawl_progress', function(data) {
            progressFill.style.width = data.progress + '%';
            progressText.textContent = data.status;
        });
        
        socket.on('crawl_complete', function(data) {
            progressFill.style.width = '100%';
            progressText.textContent = `分析完成！获取${data.total_comments}条有效评论`;
            
            // 恢复按钮
            startButton.disabled = false;
            startButton.textContent = '🚀 开始分析';
            
            // 更新统计
            commentCount.textContent = data.total_comments;
            
            // 获取最新数据并显示评论列表
            loadLatestData();
        });
        
        socket.on('crawl_error', function(data) {
            progressText.textContent = '分析失败: ' + data.error;
            startButton.disabled = false;
            startButton.textContent = '🚀 开始分析';
            
            commentsList.innerHTML = '<div class="empty-state"><div class="empty-icon">❌</div><p>分析失败，请重试</p></div>';
        });
        
        // 加载最新数据
        function loadLatestData() {
            fetch('/api/latest_data')
                .then(response => response.json())
                .then(data => {
                    if (data.comments && data.comments.length > 0) {
                        currentComments = data.comments;
                        // 更新统计数字
                        commentCount.textContent = data.comments.length;
                        displayComments(currentComments);
                    } else {
                        commentsList.innerHTML = '<div class="empty-state"><div class="empty-icon">📭</div><p>暂无评论数据</p></div>';
                    }
                })
                .catch(error => {
                    console.error('加载数据失败:', error);
                    commentsList.innerHTML = '<div class="empty-state"><div class="empty-icon">❌</div><p>数据加载失败</p></div>';
                });
        }
        
        // 显示评论列表
        function displayComments(comments) {
            if (!comments || comments.length === 0) {
                commentsList.innerHTML = '<div class="empty-state"><div class="empty-icon">📭</div><p>暂无评论数据</p></div>';
                return;
            }
            
            // 按当前排序方式排序
            const sortBy = sortSelect.value;
            const sortedComments = [...comments].sort((a, b) => {
                if (sortBy === 'replies') {
                    return b.replies - a.replies;
                } else if (sortBy === 'likes') {
                    return b.likes - a.likes;
                }
                return 0;
            });
            
            // 生成HTML
            const html = sortedComments.map((comment, index) => `
                <div class="comment-item">
                    <div class="comment-content">${escapeHtml(comment.content)}</div>
                    <div class="comment-stats">
                        <div class="stat-item">
                            <span>💬</span>
                            <span class="stat-number">${comment.replies}</span>
                            <span>回复</span>
                        </div>
                        <div class="stat-item">
                            <span>👍</span>
                            <span class="stat-number">${comment.likes}</span>
                            <span>点赞</span>
                        </div>
                    </div>
                </div>
            `).join('');
            
            commentsList.innerHTML = html;
        }
        
        // HTML转义函数
        function escapeHtml(text) {
            const div = document.createElement('div');
            div.textContent = text;
            return div.innerHTML;
        }
        
        // 排序选择器事件
        sortSelect.addEventListener('change', function() {
            if (currentComments.length > 0) {
                displayComments(currentComments);
            }
        });
        
        // 页面加载时尝试加载已有数据
        window.addEventListener('load', function() {
            loadLatestData();
        });
        
        // 回车键支持
        videoUrlInput.addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                startButton.click();
            }
        });
    </script>
</body>
</html> 